//
//  LCSendMess.m
//  TheLCNews
//
//  Created by apple on 15/12/7.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCSendMess.h"
#import <MessageUI/MessageUI.h>
@interface LCSendMess ()<MFMailComposeViewControllerDelegate>

@end

@implementation LCSendMess

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 查找当前工程有没有字符串中的这个类 / 邮件视图控制器
    Class mailClass = NSClassFromString(@"MFMailComposeViewController");
    if (mailClass != nil) {
        // 判断当前设备是否设置 邮件账户
        if ([mailClass canSendMail]) {
            //
            [self displayMailBox];
        }
        else {
            //
            [self showMailBoxOnDevice];
        }
    }
    //
    else {
        [self showMailBoxOnDevice];
    }

}

// 创建 并携带编辑好的邮件内容 模态进入邮件视图
- (void)displayMailBox {
        
    // 创建邮件视图
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    // 添加收件地址
    NSArray *recipient = [NSArray arrayWithObject:@"1332601117@qq.com"];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 邮件内容
    NSString *messageBody = @"app做的太好了";
    [mailBox setMessageBody:messageBody isHTML:YES];
    // 添加文件
    UIImage *attachedPic = [UIImage imageNamed:@""];
    NSData *data = UIImagePNGRepresentation(attachedPic);
    [mailBox addAttachmentData:data mimeType:@"" fileName:@""];
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];
}

// 进入手机系统自带的邮件工具
- (void)showMailBoxOnDevice {
    NSString *mail = @"mailto:1332601117@qq.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

// 协议方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@ 错误信息:%@", message, error);
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
